<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="年龄" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="age">
              <a-input v-model="model.age" placeholder="请输入年龄"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="性别" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="gender">
              <a-select v-model="model.gender" placeholder="请选择性别">
                <a-select-option value="">请选择</a-select-option>
                <a-select-option :value="1">男</a-select-option>
                <a-select-option :value="2">女</a-select-option>
              </a-select>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="人脸图片" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="faceImage">
              <j-image-upload v-model="model.imgPath" @change="setUserFaceImg" class="avatar-uploader" text="上传" ></j-image-upload>
            </a-form-model-item>
            <canvas style="display: none" id="myCanvas" width="100" height="100"></canvas>
            <img style="display: none" id="userFaceInfo" :src="imgSrcUrl" width="80" height="80"/>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="用户id" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="userId">
              {{model.userInfoId}}
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="用户工号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="workNo">
              <a-select :disabled="disable"
                show-search
                :value="workNo"
                placeholder="请输入工号"
                style="width: 200px"
                :default-active-first-option="false"
                :show-arrow="false"
                :filter-option="false"
                :not-found-content="null"
                @search="handleSearch"
                @change="handleChange"
                @focus="fetchUser('')"
              >
                <a-select-option v-for="d in data" :key="JSON.stringify(d)">
                  {{ d.workNo }}
                </a-select-option>
              </a-select>
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

  import { httpAction, getAction } from '@/api/manage'
  import { validateDuplicateValue } from '@/utils/util'

  export default {
    name: 'UserFaceForm',
    components: {
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        model:{
         },
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
        },
        url: {
          add: "/sys/user/faceAdd",
          edit: "sys/user/faceEdit",
          getSysUser:"/sys/user/getUserById"
        },
        workNo:'',
        data:[],
        file:'',
        imgSrcUrl:'',
        disable:false
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    created () {
       //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
    },
    methods: {
      add () {
        this.edit(this.modelDefault);
      },
      getSysUser(){
          getAction(this.url.getSysUser,{id:this.model.userInfoId}).then(res=>{
              this.model.imgPath = res.result.userFace
              this.workNo = res.result.workNo
          })
      },
      edit (record) {
        this.model = Object.assign({}, record);
        if(this.model.userInfoId!=undefined){
            this.disable = true
            this.getSysUser();
        }
        this.visible = true;
      },
      submitForm () {
        const that = this;
        this.model.groupId = 101
        this.model.file = this.file
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            httpAction(httpurl,this.model,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
            })
          }

        })
      },
        fetchUser(value) {
            /*console.log("value+++:"+value)*/
            let params = { workNo: value }
            /*console.log(params.workNo)*/
            let url = '/sys/user/getSysUserByWorkNo'
            let that = this
            getAction(url, params).then(res => {
                that.data = res.result
            })
        },
        handleSearch(value) {
            /*console.log('value:' + value)*/
            /*fetch(value, data => (this.data = data));*/
            this.fetchUser(value)
        },
        handleChange(value) {
            let user = JSON.parse(value)
            this.workNo = value
            this.model.userInfoId = user.id
            this.model.name = user.realname
            this.fetchUser(value)
        },
        setUserFaceImg(img) {
            let that = this
            this.imgSrcUrl = window._CONFIG['domianURL']+"/sys/common/static/"+img
            let image = document.getElementById("userFaceInfo");
            image.setAttribute("crossOrigin","anonymous");
            image.onload = function(){
                let canvas = document.getElementById("myCanvas");
                let ctx = canvas.getContext("2d");
                ctx.drawImage(image,0,0,100,100);
                that.file = canvas.toDataURL("image/png");
            }
        },
    }
  }
</script>